﻿using Core.Aspects;
using Core.Dtos;
using Core.Domain;
using Core.IRepository;
using Core.IServices;
using Core.Messages;
using Core.Queries;
using Library.Cache;
using Library.Helpers;
using Library.Logging;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Core.IRepository.Admin;

namespace Services
{
    [CacheName("Log")]
    public class ManageLogService : BaseService, IManageLogService
    {
        private readonly IAdminRepository<int, Log> repository;
        private readonly IAdminUnitOfWork unitOfWork;
        private readonly IGenericReaderService<int, Log, LogDto, LogQuery> readerService;
        private readonly IGenericWriterService<int, Log, LogDto, LogQuery> writerService;

        public ManageLogService(IAdminUnitOfWork unitOfWork, IAdminRepository<int, Log> repository, IGenericReaderService<int, Log, LogDto, LogQuery> readerService, IGenericWriterService<int, Log, LogDto, LogQuery> writerService)
        {
            this.repository = repository;
            this.unitOfWork = unitOfWork;
            this.readerService = readerService;
            this.writerService = writerService;
            readerService.Initialize(unitOfWork, repository);
            writerService.Initialize(unitOfWork, repository);
        }

        public Core.Messages.Response DeleteOld()
        {
            return Run(() => {
                var date = DateTime.Now.AddDays(-1);
                repository.Delete(x => x.Date.CompareTo(date) < 0);
                unitOfWork.Commit();
            });
        }

        [Cache]
        public LogDto FindByID(int id)
        {
            return readerService.FindByID(id);
        }

        public QueryResponse<LogDto> FindBy(Request<LogQuery> request)
        {
            return readerService.FindBy(request);
        }

        public Response<int> Add(Request<LogDto> dto)
        {
            return writerService.Add(dto);
        }

        [DeleteCache]
        public Response Edit(int id, Request<LogDto> dto)
        {
            return writerService.Edit(id, dto);
        }

        [DeleteCache]
        public Response Delete(int id)
        {
            return writerService.Delete(id);
        }
    }
}